import {
    NextFunction,
    Request,
    Response,
} from 'express';

import DataEntity from '../entity/DataEntity';
import BaseService from '../service/BaseService';
import BaseController from './BaseController';

/**
 * 数据查看
 */
export default abstract class BaseViewContorller<T extends DataEntity> extends BaseController<T> {

    constructor(protected service: BaseService<T>) {
        super();
    }
    /**
     * 创建实体实例
     */
    protected abstract newInstance(): T;
    /**
     * 获取数据详情
     * @param req 请求
     * @param res 返回
     * @param next 
     */
    get(req: Request, res: Response, next: NextFunction) {
        let id = req.query.id;
        if (!id) {
            res.status(400).send({
                code: 400,
                msg: '参数为空'
            });
            return;
        }
        this.service.get(parseInt(<string>id)).then((value: DataEntity | null) => {
            res.send({
                code: 200,
                msg: '',
                data: value
            })
        }).catch((err: Error) => {
            res.status(500).send({
                code: 500,
                msg: '处理失败'
            });
        });
    }
}
